package com.idom.horn.user.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.idom.horn.user.bo.impl.UserBoImpl;
import com.idom.horn.user.model.User;

public class RegisterAction implements ServletRequestAware,
		ServletResponseAware {
	private static final Logger logger = LoggerFactory
			.getLogger(RegisterAction.class);
	private UserBoImpl userImpl;

	private HttpServletRequest req;
	private HttpServletResponse resp;

	public String register() {
		User user = new User();
		String name = req.getParameter("name");
		String password = req.getParameter("password");
		if (StringUtils.isEmpty(name) || StringUtils.isEmpty(password)) {
			logger.debug("input params is illegal");
			resp.setStatus(203);
			return null;
		}
		user.setUpdateDate(new Date());
		user.setNickname(name);
		user.setPassword(password);
		logger.debug("user register,{}", user);
		userImpl.addUser(user);
		// ok
		resp.setStatus(200);
		return null;
	}

	public void setUserImpl(UserBoImpl userImpl) {
		this.userImpl = userImpl;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.req = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.resp = response;
	}
}
